/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  DIQxsqActivity.java   
 * @Package com.jiyi.activity.datainput   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2018-1-9 下午3:43:52   
 * @version V1.0     
 */  
package com.jiyi.activity.datainput;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.ConfirmPopInterface;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.ConfirmPopView;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**   
 * @ClassName:  DIQxsqActivity   
 * @Description:TODO(权限申请)   
 * @author: HYM  
 * @date:   2018-1-9 下午3:43:52   
 *      
 */
public class DIQxsqActivity extends ActivityBase implements ConfirmPopInterface{

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;
	
	@BindView(R.id.rlDcy)
	RelativeLayout rlDcy;
	@BindView(R.id.rlZg)
	RelativeLayout rlZg;
	ConfirmPopView confirmPopView;
	
	Dialog dialog;
	String assess ="";
	
	@Override
	protected void onCreate(@Nullable Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_di_qxsq);
		ButterKnife.bind(this);
		
		tvTitle.setText("申请角色权限");
		tvSearch.setVisibility(View.GONE);
	}
	
	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}
	
	@OnClick({R.id.rlDcy,R.id.rlZg,R.id.tvBack})
	public void onClick(View view){
		Intent intent ;
		switch (view.getId()) {
		case R.id.rlDcy:
			confirmPopView = new ConfirmPopView(this, this, "确认申请成为调查员吗？", "取消", "确定");
			confirmPopView.showPopWindow();
			assess = "2";
			break;
		case R.id.rlZg:
			confirmPopView = new ConfirmPopView(this, this, "确认申请成为调查主管吗？", "取消", "确定");
			confirmPopView.showPopWindow();
			assess = "1";
			break;
		case R.id.tvBack:
			finish();
			break;
		default:
			break;
		}
	}

	/**   
	 * <p>Title: OnDeleteListener</p>   
	 * <p>Description: </p>      
	 * @see ConfirmPopInterface#OnDeleteListener()
	 */ 
	@Override
	public void OnDeleteListener() {
		confirmPopView.hidePopWindow();
		doQx();
	}

	/**   
	 * <p>Title: OnCancleListener</p>   
	 * <p>Description: </p>      
	 * @see ConfirmPopInterface#OnCancleListener()
	 */ 
	@Override
	public void OnCancleListener() {
		// TODO Auto-generated method stub
		
	}

	/**   
	 * <p>Title: OnShowListener</p>   
	 * <p>Description: </p>      
	 * @see ConfirmPopInterface#OnShowListener()
	 */ 
	@Override
	public void OnShowListener() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void OnHideListener() {
		
	}
	
	
	void doQx() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("assess",assess);

		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, WebApi.DATA_INPUT_SQQX, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
//								EventBus.getDefault().post(new DISQQXEvent(Integer.parseInt(assess)));
								finish();
								
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}
}
